package top.zsyle.product.service.impl;

import io.jsonwebtoken.lang.Collections;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import top.zsyle.basic.config.BussinessException;
import top.zsyle.basic.jwt.UserInfo;
import top.zsyle.basic.util.*;
import top.zsyle.org.domain.Employee;
import top.zsyle.org.mapper.EmployeeMapper;
import top.zsyle.pet.domain.Pet;
import top.zsyle.pet.domain.PetOnlineAuditLog;
import top.zsyle.pet.domain.SearchMasterMsg;
import top.zsyle.pet.dto.AcceptDto;
import top.zsyle.pet.query.SearchMasterMsgQuery;
import top.zsyle.product.domain.Product;
import top.zsyle.product.domain.ProductDetail;
import top.zsyle.product.domain.ProductOnlineAuditLog;
import top.zsyle.product.mapper.ProductDetailMapper;
import top.zsyle.product.mapper.ProductMapper;
import top.zsyle.product.mapper.ProductOnlineAuditLogMapper;
import top.zsyle.product.service.IProductService;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-12-09
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper detailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ProductOnlineAuditLogMapper auditLogMapper;



    @Override
    public void add(Product product) {
        //1.添加服务
        //默认状态为0，下架
        product.setState(0);
        product.setCreatetime(new Date());
        product.setSalecount(0L);
        super.add(product);
        //2.添加日志
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        detailMapper.add(detail);
    }

    /**
     * 链表删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        //1.删除详情表
        detailMapper.deleteByProductId(id);
        super.delete(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        detailMapper.update(product.getDetail());
    }

    /**
     * 批量上架服务
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        //1.校验参数
        if (Collections.isEmpty(ids)){
            throw new BussinessException("上架服务不能为空！");
        }
        //2.过滤掉上架了的服务
        List<Product> products = productMapper.findByIdsAndState(ids,0);
        //3.百度AI审核
        List<ProductOnlineAuditLog> auditLogs = new ArrayList<>();
        List<Long> productIds = new ArrayList<>();
        products.forEach(pro->{
            StringBuilder sb = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(pro.getName())){
                sb.append("服务名称审核不通过，名字："+pro.getName());
            }
            String resources = pro.getResources();
            Arrays.stream(resources.split(",")).forEach(img->{
                if (!BaiduAuditUtils.ImgCensor(img)){
                    sb.append("服务图片审核不通过，名字："+pro.getName());
                }
            });
            //添加的日志
            if (sb.length()>0){
                ProductOnlineAuditLog auditLog = new ProductOnlineAuditLog();
                auditLog.setProductId(pro.getId());
                auditLog.setAuditTime(new Date());
                auditLog.setNote(sb.toString());
                auditLog.setState(0);
                //获取当前审核人
                UserInfo userInfo = ThreadLocalUtil.getUserInfo();
                String username = userInfo.getLogininfo().getUsername();
                Employee emp = employeeMapper.findByUsername(username);
                auditLog.setAuditId(emp.getId());
                auditLogs.add(auditLog);
                return;
            }
            // 修改状态
            productIds.add(pro.getId());
        });
        //通过 修改状态为上架 --- 上架时间 下架时间
        if(!Collections.isEmpty(productIds)){
            productMapper.batchUpdate(productIds);
        }
        if (!Collections.isEmpty(auditLogs)){
            //未通过 不修改状态添加日志
            auditLogMapper.batchAdd(auditLogs);
        }
    }

    /**
     * 批量下架服务
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        //1.校验参数
        if (Collections.isEmpty(ids)){
            throw new BussinessException("下架的服务不能为空哦！！");
        }
        //2.过滤掉已经下架的id
        List<Product> products = productMapper.findByIdsAndState(ids,1);
        if (Collections.isEmpty(products)){
            throw new BussinessException("服务已经下架了哦！");
        }
        List<Long> productIds = products.stream().map(Product::getId).collect(Collectors.toList());
        if (!Collections.isEmpty(productIds)){
            //修改state=0
            productMapper.offsale(productIds);
        }
    }

}
